package com.zkr.thresho.conterllor;


import com.zkr.thresho.model.*;
import com.zkr.thresho.service.ThreshoService;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by lenovo on 2021/1/8.
 */
@RestController
@CrossOrigin
public class ThreshoConterllor {
    @Autowired
    private ThreshoService threshoService;

    /**
     * 分页查询
     * @param threshoSelectVo
     * @return
     */
    @PostMapping("queryPage")
    public ResultVo queryPage(@RequestBody ThreshoSelectVo threshoSelectVo){

        return threshoService.queryPage(threshoSelectVo);
    }

    /**
     *法人下拉框接口
     * @return
     */
    @GetMapping("ofCurrentUser")
    public ResultVo queryLegalPerson(){
        //获取id
        Integer userid = 1;
        //根据id获取法人信息
        return threshoService.queryLegalPersonById(userid);
    }

    /**
     * 备件小类下拉框
     */
    @GetMapping("subclass")
    public ResultVo querySubClass(){
        return threshoService.querySubClass();
    }

    /**
     * 新增接口
     */
    @PostMapping("add")
    public ResultVo add(@RequestBody @Valid ThreshoAddVo threshoAddVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }

        List<ThreshoAddVo> threshoAddVoList = new ArrayList<>();
        threshoAddVoList.add(threshoAddVo);
        ResultVo resultVo =  threshoService.add(threshoAddVoList);

        return resultVo;

    }

    /**
     *新增-法人-备件模糊
     */
    @PostMapping("legal")
    public ResultVo legal(@RequestBody LagalSpareVo lagalSpareVo){
        if(lagalSpareVo.getLegalPersonId() == null){
            return ResultBuilderVo.error("法人id不能为空");
        }else if(lagalSpareVo.getMaterialCode() == null){
            return ResultBuilderVo.error("备件编码不能为空");
        }
        return threshoService.queryLagalSaper(lagalSpareVo);
    }


    /**
     * 新增-备件-备件信息
     */
    @PostMapping("spare")
    public ResultVo spare(SpareSelectVo spareSelectVo){
        if(spareSelectVo.getMaterialCode() == null){
            return ResultBuilderVo.error("备件编码不能为空");
        }
        return threshoService.querySpare(spareSelectVo);
    }

    /**
     * 修改
     */
    @PostMapping("update")
    public ResultVo update(@RequestBody @Valid ThreshoUpdateVo threshoUpdateVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return threshoService.update(threshoUpdateVo);


    }

    /**
     * 导入
     */
    @PostMapping("import")
    public ResultVo importFile(MultipartFile file) throws Exception{
        return threshoService.importFile(file);
    }


    /**
     * 导出
     */
    @GetMapping("export")
    public ResultVo export(String legalPersonCode,String materialCode,Integer threshonum,String remark, HttpServletResponse response)throws Exception{

        ThreshoSelectVo threshoSelectVo = new ThreshoSelectVo();
        threshoSelectVo.setLegalPersonName(legalPersonCode);
        threshoSelectVo.setMaterialCode(materialCode);
        threshoSelectVo.setThreshonum(threshonum);
        threshoSelectVo.setRemark(remark);
        return threshoService.exportFile(threshoSelectVo,response);

    }





}
